<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>编辑合同记录</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="css/layuimini.css">
    <link rel="stylesheet" href="css/public.css" media="all">
</head>
<body>
<div class="layuimini-container ">
    <div class="layuimini-main">
        <form class="layui-form " id="editContract">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">合同编号</label>
                    <div class="layui-input-block">
                        <input type="text" id="id" name="id" placeholder="" autocomplete="off"
                               class="layui-input" required lay-verify="required">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">合同名称</label>
                    <div class="layui-input-block">
                        <input type="text" id="contractName" name="contractName" placeholder="" autocomplete="off"
                               class="layui-input" >
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">客户ID</label>
                    <div class="layui-input-block">
                        <input type="text" id="custId" name="custId" placeholder=""
                               autocomplete="off"
                               class="layui-input" onkeyup="this.value=this.value.replace(/\D/g,'')"
                               onafterpaste="this.value=this.value.replace(/\D/g,'')">
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">金额</label>
                    <div class="layui-input-block">
                        <input type="text" id="money" name="money" placeholder="阿拉伯数字" autocomplete="off"
                               class="layui-input" onkeyup="this.value=this.value.replace(/\D/g,'')"
                               onafterpaste="this.value=this.value.replace(/\D/g,'')">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">开始时间</label>
                    <div class="layui-input-inline">
                        <input type="text" name="startTime" id="startTime" class="layui-input datetime">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">终止时间</label>
                    <div class="layui-input-inline">
                        <input type="text" name="endTime" id="endTime" class="layui-input datetime">
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">概述</label>
                <div class="layui-input-block">
                    <textarea type="text" id="content" name="content" placeholder="" autocomplete="off"
                              class="layui-textarea"></textarea>
                </div>
            </div>
            <div class="layui-form-item ">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="editForm">立即提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>
    </div>
</div>
<script src="lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<!--tab跳转需要layuimini-->
<script src="js/lay-module/layuimini/layuimini.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table', 'laydate', 'util'], function () {
        var form = layui.form;
        //form.render();
        var layer = layui.layer;
        var laydate = layui.laydate;
        var $ = layui.jquery;
        $('.datetime').each(function () {
            laydate.render({
                elem: this       //使用this指向当前元素,不能使用class名, 否则只有第一个有效
                , type: 'datetime'
            });
        });

        if (getQueryString("id") != null) {
            var id = getQueryString("id");
            //console.log(id);
            $.ajax({
                type: "post",
                data: {"id": id},
                url: "getOneContract",
                success: function (data) {
                    console.log(layui.util.toDateString(data.startTime, 'yyyy-MM-dd HH:mm:ss'));
                    $("#id").val(data.id);
                    $("#custId").val(data.custId);
                    $("#contractName").val(data.contractName);
                    $("#money").val(parseInt(data.money));
                    $("#startTime").val(layui.util.toDateString(data.startTime, 'yyyy-MM-dd HH:mm:ss'));
                    $("#endTime").val(layui.util.toDateString(data.endTime, 'yyyy-MM-dd HH:mm:ss'));
                    $("#content").val(data.content);
                    form.render();
                }
            });
        }

        form.on('submit(editForm)', function (data) {
            //console.log("编辑提交的信息");
            //console.log(data);
            //console.log(data.field);
            layer.confirm('确定提交吗', function (x) {
                $.ajax({
                    type: "post",
                    data: data.field,
                    url: "saveContract",
                    success: function (data) {
                        //layer.alert();
                        layer.msg(data.msg, {
                            icon: 1,
                            time: 2000 //2秒关闭（如果不配置，默认是3秒）
                        }, function () {
                            //layer.closeAll();
                            parent.layer.close(parent.layer.index);
                            parent.layui.table.reload("currentTableId", {})
                        })
                    },
                    error: function(XMLHttpRequest, textStatus, errorThrown) {
                        layer.alert(XMLHttpRequest.status + "," + "请查看选项是否填错");
                        //alert(XMLHttpRequest.readyState);
                    }
                });
            });
            return false;
        });

        function getQueryString(name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
            var r = window.location.search.substr(1).match(reg);
            //if (r != null) return unescape(r[2]);
            if (r != null) return decodeURI(r[2]);
            return null;
        }
    })
</script>
</body>
</html>
